package MainPackage;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Scanner;

import FilePackage.FileClass;
import FilePackage.Report;
import ObjectPackage.ObjectClass;

public class MenuClass {

	FileClass file = new FileClass();
	Report report = new Report();
	String fileName;
	private ArrayList<ObjectClass> arr = new ArrayList<ObjectClass>() ;
		/**
		 * input start page
		 * @param aFileName
		 */
		public void startPage(String aFileName) 
		{
			 fileName = aFileName;
			 menu();  
			 int item = -1 ;
			 item = choice();
			 if(item>0 && item<12)
				try 
			 	{
					choose(item);
				} catch (IOException e) 
				{
					e.printStackTrace();
				}
			else
			 {
				System.out.println("This item don't exit!");
				startPage(fileName);
			 }

			
		}
		
		/**
		 * Choose menu and make
		 * @param item
		 * @throws IOException
		 */
		@SuppressWarnings({ "unused", "resource" })
		private  void choose(int item) throws IOException 
		{
			file.clear();
			file.read(fileName);
			switch(item)
			{
			case 1: 
				System.out.print("-------You choose menu #1 - Loading...\n");
				file.read(fileName);
				break;
			case 2: 
				System.out.print("-------You choose menu #2 - Report by Special master \n");
				report.CreateReport(item,fileName);
				break;
			case 3: 
				System.out.print("-------You choose menu #3 - Report by Surname \n");
				report.CreateReport(item,fileName);
				break;
			case 4: 
			    System.out.print("-------You choose menu #4 - Report by Day \n");
			    report.CreateReport(item,fileName);
			    break;
			case 5: 
				System.out.print("-------You choose menu #5 - Report by Date\n");
				report.CreateReport(item,fileName);
				break;
			case 6: 
				System.out.println("-------You choose menu #6 - Search by Surname master\n");
				report.CreateReport(item,fileName);
				break;
			case 7:  
				System.out.println("-------You choose menu #7 - New record\n");
				ObjectClass ob = new ObjectClass();
				file.AddNew(ob.getNew(ob),fileName);
				break;
			case 8:  
				report.Save();
				System.out.print("-------You choose menu #8 - SAVE!");
	            break;
			case 9: 
				System.out.print("-------You choose menu #9 - Change!");
				change(); 
				break;
			case 10: 
			{
				System.out.print("-------You choose menu #10 - Delete!");
				int i=0;
				ArrayList<ObjectClass> newObject;
				print();
				System.out.println("What record do you want delete?: ");
				Scanner c = new Scanner(System.in);
				int index = c.nextInt();                                  

				   i=0;
        			for(ObjectClass obj : file.getArr())
        			{
        				obj.fillObject(obj);
        				if(i==index)
        				{
        					arr.clear();
        					arr = delete(file.getArr(), index);
        					save();
        					break;
        				}
        				i++;
        			}
        			i=0;
                	for(ObjectClass obj : arr)
        			{
        				obj.fillObject(obj);
        				System.out.println("\t"+ i+ "\t"+ obj.getSurname() + "\t" + obj.getDTime());
        				i++;
        			}

					
					
			}
				break;
			case 11: 
				System.out.print("-------You choose menu #9 - Exit!");
				close(); 
				break;
			}
			startPage(fileName);

		}
		

		/**
         * @param aObject
         * @param objectIndex
         * @return aObject
         */
        public ArrayList<ObjectClass> delete(
                        ArrayList<ObjectClass> aObject,
                        int objectIndex)
        {

        	    ObjectClass object = aObject.get(objectIndex);
        	    aObject.remove(object);
        	                   
                return aObject;

        }

        public void print()
        {
        	int i=0;
        	for(ObjectClass obj : file.getArr())
			{
				obj.fillObject(obj);
				System.out.println("\t"+ i+ "\t"+ obj.getSurname() + "\t" + obj.getDTime());
				i++;
			}
        }

		@SuppressWarnings("resource")
		private void change() {
			int i=0;
			print();
			arr.clear();
			System.out.println("Input number records, that you want change: ");
			Scanner n = new Scanner(System.in);
			Integer index = n.nextInt(); 
			for(ObjectClass obj : file.getArr())
			{
				obj.fillObject(obj);
				if(i==index)
				{
					obj.getNew(obj);
				}
				i++;
				arr.add(obj);
			}
			save();
			
		}
		/**
		 * Saving
		 */
		private void save() 
		{
			try{
	        	 FileWriter out =new FileWriter(new File(fileName),false);
	        	 try{
	        		 for(ObjectClass ob:  arr)
	           		 {
	        			 out.write(ob.ToString());
	           		     out.write(System.getProperty("line.separator"));
	           		 }
	                 
	        	 }finally{
	                 out.close();
	        	 }
	         }catch(IOException e){
	                 throw new RuntimeException(e);
	         }
		}

		/**
		 * Choose number menu
		 * @return int
		 */
		private int choice() 
		{
			System.out.print("Select the menu item. Your choose: ");
	        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
	        String menuItem = "-1";
	        try
	        {
	              menuItem = br.readLine();
	        }
	        catch(IOException ioe)
	        {
	              menuItem ="-1";
	        }
	        int selItem =-1;
	        try
	        {
	              selItem =Integer.parseInt(menuItem);
	        }
	        catch(Exception e)
	        {
	                selItem =-1;
	        }
	               
	            return selItem;
	   }
		
		/**
		 * close file
		 */
		public void close() 
		{
			System.exit(1);
		}
		
		/**
		 * input menu
		 */
		public void menu()
		{
			 System.out.print("Menu:\n 1 - Load data with next directori\n "
			+"2 - Create report by Special master\n 3 - Create report by Surname \n 4 - Create report by Day \n "
			+"5 - Search report by Date\n 6 - Search report by Surname master\n"
			+ " 7 - New record\n 8 - Save\n 9 - Change\n 10 - Delete\n 11 - Exit\n"); 
		}
		
		

	}

